package activity.setting.PlanVideo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.hichip.callback.ICameraIOSessionCallback;
import com.hichip.campro.R;
import com.hichip.content.HiChipDefines;
import com.hichip.control.HiCamera;
import com.hichip.data.HiDeviceInfo;
import com.xw.repo.BubbleSeekBar;

import base.HiActivity;
import bean.MyCamera;
import butterknife.BindView;
import common.HiDataValue;
import common.TitleView;
import utils.HiTools;

/**
 * 计划录像文件时长设置
 */
public class PlanVideoFileLenSetActivity extends HiActivity implements ICameraIOSessionCallback, View.OnClickListener {

    @BindView(R.id.title) TitleView title;
    @BindView(R.id.seekbar) BubbleSeekBar seekbar;
    private MyCamera mMyCamera;
    private HiChipDefines.HI_P2P_S_REC_AUTO_PARAM rec_param;
    @BindView(R.id.tv_time) TextView tv_time;

    private static final int GOKE_TIME = 600;
    private static final int HISI_TIME = 900;
    private int mRecordTime = HISI_TIME;

    @BindView(R.id.tv_confim) TextView tv_confim;


    @Override
    protected int setLayoutId() {
        return R.layout.activity_planvideo_filelen;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        getIntentData();
        initViewAndData();
        setListeners();
    }

    private void setListeners() {
        seekbar.setOnProgressChangedListener(new BubbleSeekBar.OnProgressChangedListener() {
            @Override
            public void onProgressChanged(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
                String str = "录像文件时长  %d  秒";
                tv_time.setText(String.format(str,progress));
            }

            @Override
            public void getProgressOnActionUp(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
            }

            @Override
            public void getProgressOnFinally(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
            }
        });
        tv_confim.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mMyCamera != null) {
            mMyCamera.registerIOSessionListener(this);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mMyCamera != null) {
            mMyCamera.unregisterIOSessionListener(this);
        }
    }

    private void getIntentData() {
        String str_uid = getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
        if (!TextUtils.isEmpty(str_uid)) {
            for (MyCamera myCamera : HiDataValue.CameraList) {
                if (myCamera.getUid().equals(str_uid)) {
                    this.mMyCamera = myCamera;
                    break;
                }
            }
        }
        if(HiTools.cameraWhetherNull(mMyCamera,this)) return;
        mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_GET_REC_AUTO_PARAM, new byte[0]);

    }

    private void initViewAndData() {
        title.setTitle("时长设置");
        title.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
        title.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int which) {
                switch (which) {
                    case TitleView.NAVIGATION_BUTTON_LEFT:
                        finish();
                        break;
                }
            }
        });

        if (mMyCamera.getChipVersion() == HiDeviceInfo.CHIP_VERSION_GOKE) {
            mRecordTime = GOKE_TIME;
        }
        seekbar.getConfigBuilder().max(mRecordTime).build();

    }

    @Override
    public void receiveSessionState(HiCamera hiCamera, int i) {

    }

    @Override
    public void receiveIOCtrlData(HiCamera arg0, int arg1, byte[] arg2, int arg3) {
        if (arg0 != mMyCamera) return;
        Bundle bundle = new Bundle();
        bundle.putByteArray(HiDataValue.EXTRAS_KEY_DATA, arg2);
        Message msg = Message.obtain();
        msg.what = HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL;
        msg.obj = arg0;
        msg.arg1 = arg1;
        msg.arg2 = arg3;
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL:
                    if (msg.arg2 == 0) {//成功
                        handIOCTRLSuccess(msg);
                    } else {//失败

                    }
                    break;
            }
        }
    };

    private void handIOCTRLSuccess(Message msg) {
        Bundle bundle = msg.getData();
        byte[] data = bundle.getByteArray(HiDataValue.EXTRAS_KEY_DATA);
        switch (msg.arg1) {
            case HiChipDefines.HI_P2P_GET_REC_AUTO_PARAM:
                rec_param = new HiChipDefines.HI_P2P_S_REC_AUTO_PARAM(data);
                String str = "录像文件时长  %d  秒";
                tv_time.setText(String.format(str,rec_param.u32FileLen));
                seekbar.getConfigBuilder().progress(rec_param.u32FileLen).build();
                break;
            case HiChipDefines.HI_P2P_SET_REC_AUTO_PARAM:
                dismissjuHuaDialog();
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_confim:
                showjuHuaDialog();
                rec_param.u32FileLen=seekbar.getProgress();
                mMyCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_REC_AUTO_PARAM,rec_param.parseContent());
                break;
        }

    }


}
